﻿using Abp.Dependency;
using Abp.Events.Bus.Handlers;
using shenghua.Order.Orders.EventDatas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Member.XP
{
    class XPEventHandler: 
        IEventHandler<AfterGenerateOrderEventData>, 
        IEventHandler<AfterCancelOrderEventData>, 
        IEventHandler<AfterFinishOrderEventData>,
        ITransientDependency
    {
        private readonly XPManager _xpManager;

        public XPEventHandler(
            XPManager xpManager
            )
        {
            _xpManager = xpManager;
        }

        public void HandleEvent(AfterGenerateOrderEventData eventData)
        {
            if(eventData.Order.Info.UsedXPValue > 0)
            {
                _xpManager.ChangeXP(eventData.Order.MemberInfo.Id, -eventData.Order.Info.UsedXPValue, "创建订单扣除积分").Wait();
            }
        }

        public void HandleEvent(AfterCancelOrderEventData eventData)
        {
            if (eventData.Order.Info.UsedXPValue > 0)
            {
                _xpManager.ChangeXP(eventData.Order.MemberInfo.Id, eventData.Order.Info.UsedXPValue, "取消订单返还积分").Wait();
            }
        }

        public void HandleEvent(AfterFinishOrderEventData eventData)
        {
            if (eventData.Order.Info.UsedXPValue > 0)
            {
                _xpManager.ChangeXP(eventData.Order.MemberInfo.Id, eventData.Order.Info.BounsXPValue, "完成订单奖励积分", true).Wait();
            }
        }
    }
}
